package com.azusa.xbiao.service.impl;

import com.azusa.xbiao.dao.GoodsDao;
import com.azusa.xbiao.dao.GoodsImageDao;
import com.azusa.xbiao.dao.GoodsMapper;
import com.azusa.xbiao.dao.UserMapper;
import com.azusa.xbiao.entity.Goods;
import com.azusa.xbiao.entity.GoodsImage;
import com.azusa.xbiao.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Transactional(rollbackFor = Exception.class)
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private GoodsImageDao imageDao;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Map<String, List<Goods>> initHome() {
        Map<String, List<Goods>> resultMap = new HashMap<>();
        resultMap.put("hot",goodsDao.selectHot());
        resultMap.put("news",goodsDao.selectHot());
        return resultMap;
    }

    @Override
    public Map<String, Object> initGoodsDetail(int gid, int uid) {
        HashMap<String, Object> resultMap = new HashMap<>();
        //查询商品具体信息
        Goods goods = goodsDao.findById(gid).get();
        //修改浏览数
        //1个小时内浏览同一件商品超过3次则不修改浏览数
        Integer look = (Integer) redisTemplate.opsForValue().get("loginUser:" + uid + ":goods:" + gid);
        if (look == null || look < 3){
            goods.setLook(goods.getLook() + 1);
            if (look == null){
                redisTemplate.opsForValue().set("loginUser:" + uid + ":goods:" + gid,1,1, TimeUnit.HOURS);
            }else {
                redisTemplate.opsForValue().set("loginUser:" + uid + ":goods:" + gid,look + 1,1, TimeUnit.HOURS);
            }
            goodsDao.save(goods);
        }
        //查询商品的图片
        String[] imageIds = goods.getCommoditPictureId().split(",");
        List<GoodsImage> images = new ArrayList<>();
        for (String imageId : imageIds) {
            images.add(imageDao.findById(Integer.parseInt(imageId)).get());
        }
        resultMap.put("images",images);
        //查询是否被用户收藏
        goods.setUserCollect(userMapper.selectUserCollect(uid, gid) == 1);
        resultMap.put("goods",goods);
        //查询热销商品
        List<Goods> hot = goodsDao.selectHot();
        resultMap.put("hot",hot);
        return resultMap;
    }

    @Override
    public void changeUserCollect(int gid, int uid, boolean flag) {
        if (flag){
            //收藏商品
            userMapper.insertCollect(uid,gid);
        }else{
            userMapper.deleteCollect(uid, gid);
        }
    }

    @Override
    public Map<String, Object> initProductList(Integer pageNum, Integer pageSize,String title) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsMapper.selectAll(title));
        List<Goods> hot = goodsDao.selectHot();
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("pageInfo",pageInfo);
        resultMap.put("hot",hot);
        return resultMap;
    }
}
